/* 
 * File:   TickTimer.h
 * Author: frank
 *
 * Created on February 15, 2015, 12:24 PM
 */

#ifndef TICKTIMER_H
#define	TICKTIMER_H

#include <stdint.h>
#include <stdbool.h>
#include "system_config.h"

// Number of clients.  Used for call back functions
#define TICKTIMER_MAXCLIENTS 3
// Number of specified tick times
#define TICKTIMER_MAXIDS 3
// Sets the interrupt priority of the tick timer from 0 to 7
#define TICKTIMER_PRIORITY 4
// Sets the sub priority of the tick timer from 0 to 3
#define TICKTIMER_SUBPRIORITY 0

#if TICKTIMER_PRIORITY == 0
    #define TICKTIMER_IP T1_INT_PRIOR_0
    #define TICKTIMER_INT ipl0
#elif TICKTIMER_PRIORITY == 1
    #define TICKTIMER_IP T1_INT_PRIOR_1
    #define TICKTIMER_INT ipl1
#elif TICKTIMER_PRIORITY == 2
    #define TICKTIMER_IP T1_INT_PRIOR_2
    #define TICKTIMER_INT ipl2
#elif TICKTIMER_PRIORITY == 3
    #define TICKTIMER_IP T1_INT_PRIOR_3
    #define TICKTIMER_INT ipl3
#elif TICKTIMER_PRIORITY == 4
    #define TICKTIMER_IP T1_INT_PRIOR_4
    #define TICKTIMER_INT ipl4
#elif TICKTIMER_PRIORITY == 5
    #define TICKTIMER_IP T1_INT_PRIOR_5
    #define TICKTIMER_INT ipl5
#elif TICKTIMER_PRIORITY == 6
    #define TICKTIMER_IP T1_INT_PRIOR_6
    #define TICKTIMER_INT ipl6
#elif TICKTIMER_PRIORITY == 7
    #define TICKTIMER_IP T1_INT_PRIOR_7
    #define TICKTIMER_INT ipl7
#endif
#if TICKTIMER_SUBPRIORITY == 0
    #define TICKTIMER_ISP T1_INT_SUB_PRIOR_0
#elif TICKTIMER_SUBPRIORITY == 1
    #define TICKTIMER_ISP T1_INT_SUB_PRIOR_1
#elif TICKTIMER_SUBPRIORITY == 2
    #define TICKTIMER_ISP T1_INT_SUB_PRIOR_3
#elif TICKTIMER_SUBPRIORITY == 3
    #define TICKTIMER_ISP T1_INT_SUB_PRIOR_3
#endif

typedef void (*TickTimer_TickCallback)(uint8_t id);

typedef struct{
    unsigned int ticks;
    unsigned int currentTicks;
    bool oneShot;
    bool active;
} TICKTIMER_DATA;

typedef struct{
    TickTimer_TickCallback tickcallback;
    uint8_t tickcallbackID;
} TICKTIMER_CLIENT;

void TickTimer_Init();
// Returns true if added
bool TickTimer_SetTickCallback(TickTimer_TickCallback function);
bool TickTimer_SetTickCallbackID(TickTimer_TickCallback function, uint8_t id);
void TickTimer_RemoveTickCallback(TickTimer_TickCallback function);

// Set a tick time
uint8_t TickTimer_SetTickTime(bool oneShot, unsigned int ms);
// Remove a tick time
void TickTimer_RemoveTickTime(uint8_t id);

// Start the tick timer
bool TickTimer_Start(uint8_t id);
void TickTimer_Stop(uint8_t id);


#endif	/* TICKTIMER_H */

